package com.smf.lsc.rest.crypt.wrapper;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
 
/**
 * @author ln
 */
public class WrapperRequest extends HttpServletRequestWrapper {
    private String requestBody = null;
    HttpServletRequest req = null;
    public WrapperRequest(HttpServletRequest request) {
        super(request);
        this.req = request;
    }
 
    public WrapperRequest(HttpServletRequest request, String requestBody) {
        super(request);
        this.requestBody = requestBody;
        this.req = request;
    }
 
 
    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new StringReader(requestBody));
    }
 
 
    @Override
    public ServletInputStream getInputStream() throws IOException {
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }
 
            @Override
            public boolean isReady() {
                return false;
            }
 
            @Override
            public void setReadListener(ReadListener readListener) {
 
            }
 
            private InputStream in = new ByteArrayInputStream(
                    requestBody.getBytes(req.getCharacterEncoding()));
 
            @Override
            public int read() throws IOException {
                return in.read();
            }
        };
    }
 
}